﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.DALFactory;
using Joyintech.IDAL.inpiao365;
using Enyim.Caching;

namespace Joyintech.BLL.inpiao365
{
    /// <summary>
    /// 业务逻辑类systemUser_BLL 的摘要说明。
    /// </summary>
    public partial class systemUser_BLL
    {
        private static systemUser_BLL instance;

        public static systemUser_BLL GetInstance()
        {
            if (instance == null)
            {
                instance = new systemUser_BLL();
            }
            return instance;
        }

        /// <summary>
        /// 将SessionId存储至Cookies
        /// </summary>
        /// <param name="SessionId"></param>
        public void StoreSessionId(string SessionId)
        {
            //不指定过期时间，则浏览器关闭自动失效
            Utils.WriteCookie("AdminSessionId", "Admin" + SessionId);
        }

        /// <summary>
        /// 获取Cookies中存储的SessionId
        /// </summary>
        /// <returns></returns>
        public string GetSessionId()
        {
            return Utils.GetCookie("AdminSessionId");
        }

        /// <summary>
        /// 获取当前登录的用户实体
        /// 实现思路：
        /// 1、获取当前浏览器用户的SessionId，此SessionId存储在Cookies中（未设置有效期，浏览器关闭则失效）
        /// 2、根据SessionId获取在Memcached中存储的当前登录用户实体
        /// 3、如果从Memcached中读取不到信息，则表示用户未登录或登录已失效
        /// </summary>
        /// <returns>获取当前登录的用户实体</returns>
        public systemUser GetCurrentSystemUser()
        {
            //获取当前用户的SessionId
            string SessionId = GetSessionId();

            if (SessionId == "")
            {
                return null;
            }

            MemcachedClient client = new MemcachedClient("enyim.com/memcached");

            //从Memcached中取出名称为Session的值
            object name = client.Get(SessionId);

            if (name == null)
            {
                return null;
            }

            return (systemUser)name;
        }

        /// <summary>
        /// 从Memcached中移出名称为Session的值
        /// </summary>
        /// <returns></returns>
        public bool ClearCurrentSystemUser()
        {
            //获取当前用户的SessionId
            string SessionId = GetSessionId();

            if (SessionId == "")
            {
                return true;
            }

            MemcachedClient client = new MemcachedClient("enyim.com/memcached");

            //从Memcached中移出名称为Session的值
            return client.Remove(SessionId);
        }

        /// <summary>
        /// 向Memcached中保存当前登录会员的实体（实体需要序列化）
        /// </summary>
        /// <param name="SessionId"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool StoreCurrentSystemUser(string SessionId, systemUser model)
        {
            MemcachedClient client = new MemcachedClient("enyim.com/memcached");

            //存值  --不带过期时间的存储，Memcached将根据LRU来决定过期策略，Memcached最大存储内存64M
            return client.Store(Enyim.Caching.Memcached.StoreMode.Set, "Admin" + SessionId, model);
        }

        /// <summary>
        /// 向Memcached中保存object
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="object"></param>
        /// <returns></returns>
        public bool StoreObject(string Key, object obj)
        {
            MemcachedClient client = new MemcachedClient("enyim.com/memcached");

            //存值  --不带过期时间的存储，Memcached将根据LRU来决定过期策略，Memcached最大存储内存64M
            return client.Store(Enyim.Caching.Memcached.StoreMode.Set, Key, obj);
        }
        
        /// <summary>
        /// 根据Key取得存储在Memcached中的值
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public object GetObject(string Key)
        {
            if (Key == "")
            {
                return null;
            }

            MemcachedClient client = new MemcachedClient("enyim.com/memcached");

            //从Memcached中取出名称为Key的值
            object obj = client.Get(Key);

            if (obj == null)
            {
                return null;
            }

            return obj;
        }

        /// <summary>
        /// 根据用户名和密码查询得到一个对象实体
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPassword"></param>
        /// <returns></returns>
        public Joyintech.Model.inpiao365.systemUser GetModel(string UserName,string UserPassword)
        {
            return dal.GetModel(UserName, UserPassword);
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserPassword"></param>
        /// <returns></returns>
        public int Update(int UserId, string UserPassword)
        {
            return dal.Update(UserId, UserPassword);
        }

    }
}
